const express = require('express')
const fs = require('fs')
const app = express()
const port = 3000
app.listen(port, () => console.log(`app listening on port ${port}!`))

// 设置全局变量
global.root = __dirname.replace(/\\/g, '/')
global.uploads = root + '/public/uploads/'

const morgan = require('morgan')
app.use(morgan('dev'))

const cors = require('cors')
app.use(cors())

const expressFormidable = require('express-formidable')
app.use(expressFormidable())

app.use(express.static('./public'))

const api = require('./router/v1/index.js')
app.use('/v1', api)


app.use((err, req, res, next) => {
    res.status(err.code || 500).send(err)
})

app.use((req, res) => {
    let data = fs.readFileSync('./error/404.html', 'utf-8')
    res.status(404).send(data)
})